<template>
  <!-- 这是产品介绍的二级目录管理 -->
  <div class="Product-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-edit" @click="handleCreate">
          新建
        </el-button>
      </div>
      <el-table
        :data="productProject"
        style="width: 100%"
      >
        <el-table-column
          prop="directoryId"
          label="ID"
          width="180"
        />
        <el-table-column
          prop="directoryName"
          label="目录名称"
          width="180"
        />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.directoryId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogForm">
      <el-form :model="form">
        <el-form-item label="项目名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.directoryName" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel('editForm')">取 消</el-button>
        <el-button type="primary" @click.native="handleUpdate('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getProduct,
  addProduct,
  modifyProduct,
  deleteProduct
} from '@/api/Product'

export default {
  data() {
    return {
      productProject: [],
      dialogForm: false,
      editForm: {
        directoryId: '',
        directoryName: ''
      },
      titleMap: {
        add: '新建',
        edit: '编辑'
      },
      dialogStatus: '',
      flag: 0
    }
  },
  created() {
    this.loadProductProject()
  },
  methods: {
    // 获取产品介绍下的二级目录列表
    name: 'ProductProject',
    loadProductProject() {
      getProduct().then(res => {
        console.log(res)
        this.productProject = res.data
      })
    },
    // 新增二级目录
    handleCreate: function(editForm) {
      this.dialogForm = true
      this.editForm.directoryName = editForm.directoryName
      this.dialogStatus = 'add'
      this.flag = 1
      console.log(this.flag)
      console.log('add')
    },
    // 编辑二级目录
    handleEdit: function(index, row) {
      this.dialogForm = true
      this.editForm = Object.assign({}, row)
      this.dialogStatus = 'edit'
      this.flag = 0
    },
    // 更新信息
    handleUpdate: function(editForm) {
      if (this.flag === 1) {
        addProduct(this.editForm).then(res => {
          console.log(res)
          this.loadProductProject()
        })
        this.$message({
          type: 'success',
          message: '创建成功'
        })
        this.dialogForm = false
      // console.log('submit')
      } else if (this.flag === 0) {
        modifyProduct(this.editForm).then(res => {
          console.log(res)
          this.loadProductProject()
        })
        this.$message({
          type: 'success',
          message: '修改成功'
        })
      }
      this.dialogForm = false
    },
    // 取消修改
    handleCancel(editForm) {
      this.$message({
        type: 'info',
        message: '已取消'
      })
      this.dialogForm = false
    },
    // 删除二级目录
    handleDelete: function(directoryId) {
      console.log(directoryId)
      this.$confirm('确认删除?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行
        console.log(directoryId)
        new Promise((resolve, reject) => {
          deleteProduct({ directoryId: directoryId }).then(res => {
          // 删除成功
            this.loadProductProject()
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style>

</style>
